<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Qt4 utility classes</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Qt4 utility classes, tutorial, C++, programming, GUI, multiplatform">
<meta name="description" content="Qt4 utility classes">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5536206-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Qt4 utility classes</h1>


<p>
In this part of the Qt4 C++ programming tutorial, we will talk about 
the utility classes available in the Qt4 library. 
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
The Qt4 library consists of a large group of helper classes, that 
help programmers to do their job. These include classes for working with 
strings, files, XML files, streams, database or network.
Here we will show only a tiny drop of the whole lake.
</p>

<p>
The Qt4 library can be used to create console and GUI applications.
In this chapter, we will illustrate some of the helper classes in console 
based applications.
</p>


<h2>Printing text to the console</h2>

<p>
This is a simple console application. The application puts some text 
into the console window.
</p>

<div class="codehead">console.cpp</div>
<pre class="code">
#include &lt;iostream&gt;

int main()
{
  std::cout &lt;&lt; "console application\n";
}
</pre>

<p>
In the first example, we print the text using the STL (Standard Template Library) library. 
</p>

<div class="codehead">console2.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
   QTextStream out(stdout);
   out &lt;&lt; "console application\n";
}
</pre>

<p>
The second example shows, how we can print text using the Qt4 programming library. 
</p>

<div class="codehead">Output</div>
<pre class="code">
console application
</pre>


<h2>QFile</h2>

<p>
The <code>QFile</code> is a class for reading from and writing to files. 
</p>

<p>
In the first example, we write a sentence to a file. 
</p>

<div class="codehead">file.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QFile&gt;

int main()
{

   QFile data("myfile");

   if (data.open(QFile::WriteOnly)) {
     QTextStream out(&amp;data);
     out &lt;&lt; "You make me want to be a better man." &lt;&lt; endl;
   }
}
</pre>

<div class="codehead">Output</div>
<pre class="code">
$ cat myfile 
You make me want to be a better man.
</pre>

<p>
The next example, we print the contents of a file to the console. The text will 
be in Hungarian language, so we must set the correct codec. 
</p>

<div class="codehead">szerelem</div>
<pre class="code">
S a régi szeretőmér­
mit nem cselekednék,
tengerből a vizet
kanállal lemerném.

S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék. 
</pre>

<div class="codehead">szerelem.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QFile&gt;

int main()
{
  QFile data("szerelem");

  QString line;

  if (data.open(QFile::ReadOnly)) {
    QTextStream in(&amp;data);
    QTextStream out(stdout);

    out.setCodec("UTF-8");
    in.setCodec("UTF-8");

    do {
      line = in.readLine();
      out &lt;&lt; line &lt;&lt; endl;
    } while (!line.isNull());
  }
}
</pre>

<div class="codehead">Output</div>
<pre class="code">
 S a régi szeretőmér­
mit nem cselekednék,
tengerből a vizet
kanállal lemerném.

S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék. 
</pre>


<h2>QList</h2>

<p>
The <code>QList</code> is one of the generic Qt4's containers. 
It is used to store a list of values and provides fast index-based access 
as well as fast insertions and removals.
</p>

<div class="codehead">mlist.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QList&gt;

int main()
{
  QTextStream out(stdout);

  QList&lt;QString&gt; list;

  list &lt;&lt; "Balzac" &lt;&lt; "Tolstoy" &lt;&lt; "Guldbrassen"
       &lt;&lt; "London" &lt;&lt; "Galsworthy" &lt;&lt; "Sienkiewicz";

  qSort(list);

  for (int i = 0; i &lt; list.size(); ++i) {
    out &lt;&lt; list.at(i) &lt;&lt; endl;
  }

}
</pre>

<p>
In the code example, we create a list of great novelists. We sort 
alphabetically the list and print it to the
console. 
</p>

<div class="codehead">Output</div>
<pre class="code">
Balzac
Galsworthy
Guldbrassen
London
Sienkiewicz
Tolstoy
</pre>


<h2>QDir</h2>

<p>
The <code>QDir</code> class provides access to directory 
structures and their contents.
</p>

<div class="codehead">home.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QDir&gt;

int main()
{
  QTextStream out(stdout);
  QString home = QDir::homePath();
  out &lt;&lt; home &lt;&lt; endl;
}
</pre>

<p>
This example determines the home directory and prints it to the console. 
</p>

<div class="codehead">Output</div>
<pre class="code">
/home/vronskij
</pre>

<p>
In the following example, we use the QDir class
</p>

<div class="codehead">filters.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QDir&gt;


int main()
{
  QTextStream out(stdout);
  QDir dir;

  QStringList filters;
  filters &lt;&lt; "*.c" &lt;&lt; "*.c~";
  dir.setNameFilters(filters);

  QFileInfoList list = dir.entryInfoList();

  for (int i = 0; i &lt; list.size(); ++i) {
    QFileInfo fileInfo = list.at(i);
    out &lt;&lt; QString("%1").arg(fileInfo.fileName());
    out &lt;&lt; endl;
  } 
}
</pre>

<p>
The code example determines all files in the current directory and 
applies a specific filter to the files. 
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ls -F
anim*  anim.c  anim.c~  filters*
$ ./filters 
anim.c
anim.c~
</pre>


<h2>QTime</h2>

<p>
The <code>QTime</code> class provides clock time functions.
</p>

<p>
In the following example, we print the current local time to the console. 
</p>

<div class="codehead">mtime.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;
#include &lt;QTime&gt;

int main()
{
   QTextStream out(stdout);

   QTime qtime = QTime::currentTime();
   QString stime = qtime.toString(Qt::LocalDate);

   out &lt;&lt; stime &lt;&lt; endl;
}
</pre>

<p>
Watch out that the file must not be called time.cpp. 
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./time 
10:30:33 PM
</pre>


<h2>QString</h2>

<p>
The <code>QString</code> class is used to work with strings. 
This is probably the most important utility class
available in Qt4 programming library. 
</p>

<div class="codehead">concat.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
   QString a = "Disziplin ";
   QString b = "ist ";
   QString c = "Macht.\n";

   QTextStream out(stdout);
   out &lt;&lt; a + b + c;
}
</pre>

<p>
The first example concatenates three strings. 
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./concat
Disziplin ist Macht.
</pre>

<p>
The seconds example builds a string by means of appending 
text one after the other. 
</p>

<div class="codehead">append.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{

   QString string = "Whether I shall ";

   string.append("turn out to be the hero of my own life, \n");
   string.append("or whether that station will be held by anybody else, \n");
   string.append("these pages must show.\n");

   QTextStream out(stdout);
   out &lt;&lt; string;
}
</pre>


<div class="codehead">Output</div>
<pre class="code">
$ ./append
Whether I shall turn out to be the hero of my own life, 
or whether that station will be held by anybody else, 
these pages must show.
</pre>

<p>
The next example shows an argument substitution. 
</p>

<div class="codehead">arg.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
   QString string = "What if I gave you %1 red roses?";
   int num = 21;

   QTextStream out(stdout);
   out &lt;&lt; string.arg(num) &lt;&lt; endl; 
}
</pre>

<div class="codehead">Output</div>
<pre class="code">
$ ./str3 
What if I gave you 21 red roses?
</pre>

<p>
The following example shows, how we can determine the size of the string. 
</p>

<div class="codehead">size.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
 QString string = "The history of my life.";

 QTextStream out(stdout);
 out &lt;&lt; "The string has " + QString::number(string.size())
    + " characters." &lt;&lt; endl; 
}
</pre>

<div class="codehead">Output</div>
<pre class="code">
 ./size
The string has 23 characters.
</pre>


<p>
The last example is about making a string uppercase or lowercase.
</p>

<div class="codehead">uplow.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
  QString string = "The history of my life.";

  QTextStream out(stdout);
  out &lt;&lt; string.toLower() &lt;&lt; endl; 
  out &lt;&lt; string.toUpper() &lt;&lt; endl; 
}
</pre>

<div class="codehead">Output</div>
<pre class="code">
$ ./uplow
the history of my life.
THE HISTORY OF MY LIFE.
</pre>


<p>
In this chapter, we have described some utility classes in Qt4.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified May 20, 2008  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

